package com.example.spring2301.service;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AspectJByAno {

    @Pointcut("execution(* com.example.spring2301.service.*.*(..))")
    public void pointCut(){

    }

    @Before("pointCut()")
    public void myBefore(JoinPoint joinPoint) {
        System.out.println("前置通知执行*****");
        System.out.println("目标类：" + joinPoint.getTarget());
        System.out.println("增强的方法：" + joinPoint.getSignature().getName());
    }

    @Around("pointCut()")
    public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕通知开始执行*****");
        Object obj = joinPoint.proceed();
        System.out.println("环绕通知结束*****");

        return obj;
    }

    @After("pointCut()")
    public void  myAfter(JoinPoint joinPoint) {
        System.out.println("后置通知开始*********");
        System.out.println("后置目标类=" + joinPoint.getTarget());
        System.out.println("后置增强的目标：" + joinPoint.getSignature().getName());
    }

    @AfterReturning("pointCut()")
    public void myAfterReturn(JoinPoint joinPoint) {
        System.out.println("后置返回通知*****");
        System.out.println("增强的方法处理为：" + joinPoint.getSignature().getName());
    }

    @AfterThrowing(value = "pointCut()",throwing = "e")
    public void myThrowing(JoinPoint joinPoint,Throwable e) {
        System.out.println("异常通知*****，异常信息=" + e.getMessage());
    }
}
